package com.xinchao.ck_mips.view;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.os.Process;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.xinchao.ck_mips.R;
import com.xinchao.ck_mips.logic.PlayLogic;
import com.xinchao.ck_mips.presenter.BindPresenter;
import com.xinchao.ck_mips.utils.DeviceUtil;
import com.xinchao.ck_mips.utils.DialogUtils;
import com.xinchao.ck_mips.utils.NetWorkUtil;
import com.xinchao.rxtools.rxhttp.tools.IDisposable;

/**
 * Created by wq on 2019/4/23.
 */
public class BindActivity extends BaseMvpActivity<IBindView, BindPresenter> implements IBindView, TextWatcher, View.OnClickListener {
    private Button btnCommit;
    private TextView tvError;
    private EditText editCode;
    private ImageView imgCode;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (DeviceUtil.isLandscape(this)) {
            //横屏
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
        } else {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
        }
        setContentView(R.layout.activity_bind);
        initView();
        if (Build.MODEL.contains("ZLP86Ni") || Build.MODEL.contains("IoT3288") || Build.MODEL.contains("3280") || Build.MODEL.contains("DS835")) {
            PlayLogic.getInstance().setLocalOnOffTime(this);
        }
        //轮询请求注册终端
        mPresenter.repeatRegister();
    }

    @Override
    protected BindPresenter createPrestener() {
        return new BindPresenter();
    }

    /**
     * 初始化界面组件
     */
    private void initView() {
        btnCommit = findViewById(R.id.btnCommit);
        tvError = findViewById(R.id.tvError);
        editCode = findViewById(R.id.editCode);
        imgCode = findViewById(R.id.imgCode);
        editCode.addTextChangedListener(this);
        btnCommit.setOnClickListener(this);
        //设置二维码
        mPresenter.setBtp();

    }

    @Override
    public void showLoading() {
        //加载
        showUILoading();
    }

    @Override
    public void hideLoading() {
        //隐藏
        hideUILoading();
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    public void gotoPlay() {
        Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();
        startActivity(new Intent(this, PlayActivity.class));
        finish();

    }

    @Override
    public void showToastError(String errorInfo) {
        Toast.makeText(this, errorInfo, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void setErrorText(boolean isShow, String tips) {
        if (isShow) {
            tvError.setVisibility(View.VISIBLE);
            tvError.setText(tips);
            editCode.setBackgroundResource(R.drawable.shape_edit_red);
        } else {
            Toast.makeText(this, tips, Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    public void setZxingBtp(Bitmap btp) {
        imgCode.setImageBitmap(btp);
    }

    @Override
    public String getEditText() {
        return editCode.getText().toString();
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        tvError.setVisibility(View.INVISIBLE);
        editCode.setBackgroundResource(R.drawable.shape_edit_default);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnCommit:
                if (!TextUtils.isEmpty(editCode.getText().toString()) && editCode.getText().toString().length() == 8) {
                    if (!NetWorkUtil.isNetworkAvailable(this)) {
                        Toast.makeText(this, "设备网络异常，请检查网络", Toast.LENGTH_LONG).show();
                        return;
                    }
                    mPresenter.bindDevice(true);
                } else if (TextUtils.isEmpty(editCode.getText().toString())) {
                    setErrorText(true, "注册码为空");
                } else if (editCode.getText().toString().length() != 8) {
                    setErrorText(true, "请输入8位注册码");
                }
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        removeAllDisposable();
        compositeDisposable = null;
        mPresenter.detachView();
    }

    @Override
    public void onBackPressed() {
        onDestroy();
        Process.killProcess(Process.myPid());
    }

    @Override
    public IDisposable getDisposable() {
        return this;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Log.v("xika", "菜单键");
        if (keyCode == KeyEvent.KEYCODE_MENU) {
            DialogUtils.showInfoDialog(this);
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }
}
